<?php
class Ueditorapi extends Base_Controller
{
    public $config;
    public function __construct()
    {
        parent::__construct();
        $this->load->model('ueditor_model');
        $this->config = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents(APPPATH."config/ueditor.json")), true);
    }

    public function upload()
    {
        $action = $this->input->get('action');
        switch ($action) {
            case 'config':
                $result =  json_encode($this->config);
                break;

            /* 上传图片 */
            case 'uploadimage':
            /* 上传涂鸦 */
            case 'uploadscrawl':
            /* 上传视频 */
            case 'uploadvideo':
            /* 上传文件 */
            case 'uploadfile':
            /* 抓取远程文件 */
            case 'catchimage':
                $result = $this->ueditor_model->upload($action,$this->config);
                break;
            /* 列出图片 */
            case 'listfile':
            /* 列出文件 */
            case 'listimage':
                $size = $this->input->get('size');
                $start = $this->input->get('start');
                $result = $this->ueditor_model->getlist($action,$this->config,$size,$start);
                break;
            default:
                $result = json_encode(array(
                    'state'=> '请求地址出错'
                ));
                break;
        }

        /* 输出结果 */
        if (isset($_GET["callback"])) {
            if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
                echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
            } else {
                echo json_encode(array(
                    'state'=> 'callback参数不合法'
                ));
            }
        } else {
            echo $result;
        }
    }
}